<template>
    <div class="confirmOrder">
        <!-- 头部 -->
        <div class="confirmOrder-header">
            <div class="header-content">
                <p>
                    <i class="el-icon-s-order"></i>
                </p>
                <p>确认订单</p>
                <router-link to></router-link>
            </div>
        </div>
        <!-- 头部END -->

        <!-- 主要内容容器 -->
        <div class="content">
            <!-- 选择地址 -->
            <div class="section-address">
                <p class="title">收货地址</p>
                <div class="address-body">
                    <el-radio-group v-model="confirmAddress" @change="onChangeRadio">
                        <el-radio :label="item.addressId"
                                  v-for="item in address"
                                  :key="item.addressId"
                                  border
                        >
                            <h2>{{item.userName}}</h2>
                            <p class="phone">{{item.tel}}</p>
                            <p class="address">{{item.streetName}}</p>
                        </el-radio>
                    </el-radio-group>
                </div>
            </div>
            <!-- 选择地址END -->

            <!-- 商品及优惠券 -->
            <div class="section-goods">
                <p class="title">商品及优惠券</p>
                <div class="goods-list">
                    <ul>
                        <li v-for="item in getCheckGoods" :key="item.id">
                            <el-image :src="item.productImg" style="width: 80px;height: 80px;float: left"></el-image>
                            <span class="pro-name">{{item.productName}}</span>
                            <span class="pro-price">{{item.salePrice}}元 x {{item.productNum}}</span>
                            <span class="pro-status"></span>
                            <span class="pro-total">{{item.salePrice * item.productNum}}元</span>
                        </li>
                    </ul>
                </div>
            </div>
            <!-- 商品及优惠券END -->

            <!-- 配送方式 -->
            <div class="section-shipment">
                <p class="title">配送方式</p>
                <p class="shipment">包邮</p>
            </div>
            <!-- 配送方式END -->

            <!-- 发票 -->
            <div class="section-invoice">
                <p class="title">发票</p>
                <p class="invoice">电子发票</p>
                <p class="invoice">个人</p>
                <p class="invoice">商品明细</p>
            </div>
            <!-- 发票END -->

            <!-- 结算列表 -->
            <div class="section-count">
                <div class="money-box">
                    <ul>
                        <li>
                            <span class="title">商品件数：</span>
                            <span class="value">{{getCheckNum}}件</span>
                        </li>
                        <li>
                            <span class="title">商品总价：</span>
                            <span class="value">{{getTotalPrice}}元</span>
                        </li>
                        <li>
                            <span class="title">活动优惠：</span>
                            <span class="value">-0元</span>
                        </li>
                        <li>
                            <span class="title">优惠券抵扣：</span>
                            <span class="value">-0元</span>
                        </li>
                        <li>
                            <span class="title">运费：</span>
                            <span class="value">0元</span>
                        </li>
                        <li class="total">
                            <span class="title">应付总额：</span>
                            <span class="value">
                <span class="total-price">{{getTotalPrice}}</span>元
              </span>
                        </li>
                    </ul>
                </div>
            </div>
            <!-- 结算列表END -->

            <!-- 结算导航 -->
            <div class="section-bar">
                <div class="btn">
                    <router-link to="/shoppingCart" class="btn-base btn-return">返回购物车</router-link>
                    <a href="javascript:void(0);" @click="addOrder" class="btn-base btn-primary">结算</a>
                </div>
            </div>
            <!--支付订单-->
            <el-dialog title="支付" width="400px" center :visible.sync="showPay">
                <el-form :model="payForm" :rules="payFormRules" status-icon ref="payForm"
                         class="demo-ruleForm">
                    <el-form-item prop="balance" label="应付总额">
                        <li class="total">
                            <span class="value">
                <span class="dialog-total-price">{{getTotalPrice}}元</span>
              </span>
                        </li>
                    </el-form-item>
                    <el-form-item prop="payType" label="付款方式">
                        <el-radio v-model="payForm.payType" label="WeChat">
                            <i><img src="../assets/images/inchageIcon/wechat.png" alt="微信支付"></i>
                        </el-radio>
                        <el-radio v-model="payForm.payType" label="AliPay">
                            <i><img src="../assets/images/inchageIcon/alipay.png" alt="支付宝"></i>
                        </el-radio>
                        <el-radio v-model="payForm.payType" label="ZmallPay">
                            <i><img src="../assets/images/inchageIcon/zmallpay.png" alt="zmall支付"></i>
                        </el-radio>
                    </el-form-item>
                    <el-form-item>
                        <el-button size="medium" type="primary" @click="createOrder('payForm')" style="width:45%;">支付
                        </el-button>
                        <el-button size="medium" type="danger" @click="orderRedis('payForm')" style="width:45%;">稍后支付
                        </el-button>
                    </el-form-item>
                </el-form>
            </el-dialog>
            <!--支付订单END -->
            <!-- 结算导航END -->
        </div>
        <!-- 主要内容容器END -->
    </div>
</template>

<script>
    import {mapGetters} from "vuex";
    import {mapActions} from "vuex";
    import {CodeToText, regionData} from "element-china-area-data";

    export default {
        name: "ConfirmOrder",
        data() {
            return {
                confirmAddress: 7, // 选择的地址id
                // 地址列表
                address: [],//用户收货地址列表
                radioDefault: false,//是否设置为默认地址
                selectOptions: [],
                options: regionData,
                showADD: false,//是否展示添加地址表单
                payForm: {
                    balance: "",
                    payType: null
                },
                payFormRules: {
                    payType: [{required: true, message: '请选择支付方式', trigger: "blur"}]
                },
                showPay: false,//支付弹窗
                goodsList: "",
            };
        },
        computed: {
            // 结算的商品数量; 结算商品总计; 结算商品信息
            ...mapGetters(["getCheckNum", "getTotalPrice", "getCheckGoods", "getUserAddress"])
        },
        methods: {
            ...mapActions(["deleteShoppingCart", "setUserAddress", "setShoppingCart"]),
            //展示添加弹窗
            showAddDialog() {
                this.showADD = true;
            },
            //展示支付窗口
            addOrder() {
                this.showPay = true;
            },
            //创建订单
            createOrder(formName) {
                this.$refs[formName].validate((valid) => {
                    //如果通过校验开始登录
                    if (valid) {
                        this.$axios
                            .post("/item/preorder", {
                                "addressId": this.confirmAddress,
                                "cartProducts": this.goodsList,
                                "payFlag": true,
                                "payType": this.payForm.payType,
                                "userId": this.$store.getters.getUser.id,
                                "userName": this.$store.getters.getUser.username
                            })
                            .then(res => {
                                if (res.data.result == -1) {
                                    this.notifyError("请先充值");
                                  setTimeout(() => {
                                    this.$router.push("/personalcenter");
                                  }, 1500)
                                } else if (res.data.result == -2) {
                                    this.notifyError("您的余额不足请先充值");
                                  setTimeout(() => {
                                    this.$router.push("/personalcenter");
                                  }, 1500)
                                } else if (res.data.result == -3) {
                                    this.notifyError("对不起商品库存不足，请先浏览其他商品");
                                  setTimeout(() => {
                                    this.$router.push("/commodity");
                                  }, 1500)
                                } else {
                                    this.notifySucceed("支付成功");
                                    this.getUserCarList();
                                    this.showPay = false;
                                    setTimeout(() => {
                                        this.$router.push("/personalcenter");
                                    }, 1500)
                                }

                            })
                    } else {
                        this.notifyError("请选择付款方式");
                    }
                });
            },
            //稍后支付
            orderRedis(formName) {
                this.$refs[formName].validate((valid) => {
                    //如果通过校验开始登录
                    if (valid) {
                        this.$axios
                            .post("/item/preorder", {
                                "addressId": this.confirmAddress,
                                "cartProducts": this.goodsList,
                                "payFlag": false,
                                "payType": this.payForm.payType,
                                "userId": this.$store.getters.getUser.id,
                                "userName": this.$store.getters.getUser.username
                            })
                            .then(res => {
                                this.showPay = false;
                                this.getUserCarList();
                                setTimeout(() => {
                                    this.$router.push("/personalcenter");
                                }, 1500)


                            })
                    } else {
                        this.notifyError("请选择付款方式");
                    }
                });
            },
            //获取用户地址
            getAddressList() {
                this.$axios
                    .get("/item/getlistaddress", {
                        params: {
                            "userId": this.$store.getters.getUser.id
                        }
                    })
                    .then(res => {
                        this.address = res.data.result;
                        this.setUserAddress(res.data.result);
                    })
                    .catch(err => {
                        return Promise.reject(err);
                    })

            },
            //获取用户购物车信息
            getUserCarList() {
                var _this = this;
                let params = new URLSearchParams();
                params.append("userId", this.$store.getters.getUser.id);
                this.$axios
                    .post("/member/cartList", params)
                    .then(res => {
                        _this.setShoppingCart(res.data.result);
                    })
                    .catch(err => {
                        return Promise.reject(err);
                    });
            },
            onChangeRadio(val){
                this.confirmAddress = val;
            },
        },
        created() {
            // 如果没有勾选购物车商品直接进入确认订单页面,提示信息并返回购物车
            if (this.getCheckNum < 1) {
                this.notifyError("请勾选商品后再结算");
                setTimeout(() => {
                    this.$router.push({path: "/shoppingCart"});
                },1000)
            }
            this.getAddressList();
            this.goodsList = this.getCheckGoods;
            if (this.$store.getters.getUserAddress.length === 0){
                this.notifyError('请先添加一个收货地址')
                setTimeout(() => {
                    this.$router.push({path: "/personalcenter/addAddress"});
                },1000)
            }
        },

    }
</script>

<style scoped>
    .confirmOrder {
        background-color: #f5f5f5;
        padding-bottom: 20px;
    }

    /* 头部CSS */
    .confirmOrder .confirmOrder-header {
        background-color: #fff;
        border-bottom: 2px solid #ff6700;
        margin-bottom: 20px;
    }

    .confirmOrder .confirmOrder-header .header-content {
        width: 1225px;
        margin: 0 auto;
        height: 80px;
    }

    .confirmOrder .confirmOrder-header .header-content p {
        float: left;
        font-size: 28px;
        line-height: 80px;
        color: #424242;
        margin-right: 20px;
    }

    .confirmOrder .confirmOrder-header .header-content p i {
        font-size: 45px;
        color: #ff6700;
        line-height: 80px;
    }

    /* 头部CSS END */

    /* 主要内容容器CSS */
    .confirmOrder .content {
        /*width: 1225px;*/
        max-width: 90%;
        margin: 0 auto;
        padding: 48px 0 0;
        background-color: #fff;
    }

    /* 选择地址CSS */
    .confirmOrder .content .section-address {
        margin: 0 48px;
        overflow: hidden;
    }

    .confirmOrder .content .section-address .title {
        color: #333;
        font-size: 18px;
        line-height: 20px;
        margin-bottom: 20px;
    }

    .confirmOrder .content .address-body li {
        float: left;
        color: #333;
        width: 220px;
        height: 178px;
        border: 1px solid #e0e0e0;
        padding: 15px 24px 0;
        margin-right: 17px;
        margin-bottom: 24px;
    }

    .confirmOrder .content .address-body .el-radio{
        width: 260px;
        height: 180px;
        line-height: 30px;
    }

    .confirmOrder .content .address-body .in-section {
        border: 1px solid #ff6700;
    }

    .confirmOrder .content .address-body .el-radio h2 {
        font-size: 18px;
        font-weight: normal;
        line-height: 30px;
        margin-bottom: 10px;
    }

    .confirmOrder .content .address-body .el-radio p {
        font-size: 14px;
        color: #757575;
    }

    .confirmOrder .content .address-body .el-radio .address {
        padding: 10px 0;
        max-width: 260px;
        max-height: 88px;
        line-height: 22px;
        table-layout: fixed;
        /*white-space:pre-wrap;*/
        /*word-break: normal;*/
        overflow: hidden;
    }

    .confirmOrder .content .address-body .add-address {
        text-align: center;
        line-height: 30px;
    }

    .confirmOrder .content .address-body .add-address i {
        font-size: 30px;
        padding-top: 50px;
        text-align: center;
    }

    /* 选择地址CSS END */

    /* 商品及优惠券CSS */
    .confirmOrder .content .section-goods {
        margin: 0 48px;
    }

    .confirmOrder .content .section-goods p.title {
        color: #333;
        font-size: 18px;
        line-height: 40px;
    }

    .confirmOrder .content .section-goods .goods-list {
        padding: 5px 0;
        border-top: 1px solid #e0e0e0;
        border-bottom: 1px solid #e0e0e0;
    }

    .confirmOrder .content .section-goods .goods-list li {
        padding: 10px 0;
        color: #424242;
        overflow: hidden;
    }

    .confirmOrder .content .section-goods .goods-list li .pro-name {
        font-size: 22px;
        float: left;
        width: 650px;
        line-height: 30px;
    }

    .confirmOrder .content .section-goods .goods-list li .pro-price {
        font-size: 18px;
        float: left;
        width: 150px;
        text-align: center;
        line-height: 30px;
    }

    .confirmOrder .content .section-goods .goods-list li .pro-status {
        float: left;
        width: 99px;
        height: 30px;
        text-align: center;
        line-height: 30px;
    }

    .confirmOrder .content .section-goods .goods-list li .pro-total {
        font-size: 20px;
        width: 190px;
        text-align: center;
        color: #ff6700;
        line-height: 30px;
    }

    /* 商品及优惠券CSS END */

    /* 配送方式CSS */
    .confirmOrder .content .section-shipment {
        margin: 0 48px;
        padding: 25px 0;
        border-bottom: 1px solid #e0e0e0;
        overflow: hidden;
    }

    .confirmOrder .content .section-shipment .title {
        float: left;
        width: 150px;
        color: #333;
        font-size: 18px;
        line-height: 38px;
    }

    .confirmOrder .content .section-shipment .shipment {
        float: left;
        line-height: 38px;
        font-size: 14px;
        color: #ff6700;
    }

    /* 配送方式CSS END */

    /* 发票CSS */
    .confirmOrder .content .section-invoice {
        margin: 0 48px;
        padding: 25px 0;
        border-bottom: 1px solid #e0e0e0;
        overflow: hidden;
    }

    .confirmOrder .content .section-invoice .title {
        float: left;
        width: 150px;
        color: #333;
        font-size: 18px;
        line-height: 38px;
    }

    .confirmOrder .content .section-invoice .invoice {
        float: left;
        line-height: 38px;
        font-size: 14px;
        margin-right: 20px;
        color: #ff6700;
    }

    /* 发票CSS END */

    /* 结算列表CSS */
    .confirmOrder .content .section-count {
        margin: 0 48px;
        padding: 20px 0;
        overflow: hidden;
    }

    .confirmOrder .content .section-count .money-box {
        float: right;
        text-align: right;
    }

    .confirmOrder .content .section-count .money-box .title {
        float: left;
        width: 126px;
        height: 30px;
        display: block;
        line-height: 30px;
        color: #757575;
    }

    .confirmOrder .content .section-count .money-box .value {
        float: left;
        min-width: 105px;
        height: 30px;
        display: block;
        line-height: 30px;
        color: #ff6700;
    }

    .confirmOrder .content .section-count .money-box .total .title {
        padding-top: 15px;
    }

    .confirmOrder .content .section-count .money-box .total .value {
        padding-top: 10px;
    }

    .confirmOrder .content .section-count .money-box .total-price {
        font-size: 30px;
    }

    /* 结算列表CSS END */

    /* 结算导航CSS */
    .confirmOrder .content .section-bar {
        padding: 20px 48px;
        border-top: 2px solid #f5f5f5;
        overflow: hidden;
    }

    .confirmOrder .content .section-bar .btn {
        float: right;
    }

    .confirmOrder .content .section-bar .btn .btn-base {
        float: left;
        margin-left: 30px;
        width: 158px;
        height: 38px;
        border: 1px solid #b0b0b0;
        font-size: 14px;
        line-height: 38px;
        text-align: center;
    }

    .confirmOrder .content .section-bar .btn .btn-return {
        color: rgba(0, 0, 0, 0.27);
        border-color: rgba(0, 0, 0, 0.27);
    }

    .confirmOrder .content .section-bar .btn .btn-primary {
        background: #ff6700;
        border-color: #ff6700;
        color: #fff;
    }

    /* 结算导航CSS */

    .dialog-total-price {
        font-size: 20px;
        color: #ff6700;
    }

    /* 主要内容容器CSS END */
</style>
